<?php

namespace App\Console\Commands\OpenShop;

use App\Models\Common\MapNationalUniversitiesModel;
use GuzzleHttp\Client;
use Illuminate\Console\Command;

class ImportSchoolAoiGeo extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'openshop:import-school-aoi-geo';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '导入高校周边数据';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $fileUrl = 'https://file.51kuafu.com/7f/dc/061145693ace2f0937b81ca0b09da934dc4f90ea4e861c38c74f5bca657a.geojson';
        // 获取在线文件内容
        $count    = 0;
        $tempData = (new Client())->request("get", $fileUrl)->getBody()->getContents();
        foreach (json_decode($tempData, true)['features'] as $feature) {
            $schoolName = $feature['properties']['name'];
            MapNationalUniversitiesModel::query()
                ->where('name', $schoolName)
                ->update([
                    'boundary' => json_encode($feature['geometry']['coordinates'][0][0])
                ]);
            $this->info($count);
            $count++;
        }
    }
}
